// pages/personal/personal.js
var utils = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    available_predeposit: 0,
    couponnum: 0,
    menu: [
      // {
      //   icon: 'http://xinong.qidian2018.com/smimgs/p1.png',
      //   name: '我的通知',
      //   path: '/pages/personal/mynotice/mynotice',
      // },
      {
        icon: 'http://xinong.qidian2018.com/smimgs/p2.png',
        name: '我的拼购',
        path: '/pages/personal/spellpurchase/spellpurchase',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/kill-price.png',
        name: '我的砍价',
        path: '/pages/killPrice/lists',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p7.png',
        name: '我的众筹',
        path: '/pages/personal/mycrowdraising/mycrowdraising',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p3.png',
        name: '我的优惠券',
        path: '/pages/personal/personalcoupon/personalcoupon',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p4.png',
        name: '我的地址',
        path: '/pages/personal/myaddress/myaddress?typeaddress=1',
      }, {
        icon: 'https://xinong.qidian2018.com/smimgs/comment.png',
        name: '我的评论',
        path: '/pages/personal/commentlist/commentlist',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p5.png',
        name: '我要加盟',
        path: '/pages/personal/wantjoin/wantjoin',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p6.png',
        name: '邀请有奖',
        path: '/pages/personal/myinvitation/myinvitation',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/jinbei.png',
        name: '进呗',
        path: '/pages/stock/stock/form',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p11.png',
        name: '商户入口',
        path: '/pages/login/login?logintype=1',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p10.png',
        name: '果农入口',
        path: '/pages/login/login?logintype=2',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p8.png',
        name: '问题反馈',
        path: '/pages/personal/myfeedback/myfeedback',
      }, {
        icon: 'http://xinong.qidian2018.com/smimgs/p9.png',
        name: '联系客服',
        path: '/pages/personal/contact/contact',
      },


    ],
  },
  onLoad: function(options) {
    var that = this;
    that.personalInfo();
    that.onOrderStateNum();
    that.allLogistics();
  },
  onShow: function() {
    var that = this;
    utils.setTabBarBadge();
    that.onOrderStateNum();
    that.personalInfo();
  },
  toMenuItemDetails: function(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var path = that.data.menu[index].path;
    wx.navigateTo({
      url: path,
      fail: function() {
        wx.switchTab({
          url: path,
        });
      }
    });
  },
  onOrderStateNum: function(e) {
    var that = this;
    utils.http("/api/v1/order/order_state_num", {}, function(res) {
      that.setData({
        stateNum: res.datas,
      })
    })
  },
  allLogistics: function(e) {
    var that = this;
    utils.http("/api/v1/order/all_order_logistics", {}, function(res) {
      that.setData({
        logistics: res.datas,
      })
    })
  },
  //我的个人信息
  personalInfo: function(e) {
    var that = this;
    utils.http("/api/v1/member/index", {}, function(res) {
      that.setData({
        info: res.datas,
        available_predeposit: res.datas.available_predeposit,
        couponnum: res.datas.coupon_num
      })
    })
  },
  //开通会员页面
  onOpenVipTap: function(e) {
    var that = this;
    that.setData({
      ishidden: true
    })
    if (that.data.info.is_vip == 1) {
      wx.switchTab({
        url: '../vipmember/vipmember',
      })
    } else {
      wx.navigateTo({
        url: '../openvip/openvip?viptype=0'
      })
    }
  },
  //授权登陆
  bindGetUserInfo: function(e) {
    var that = this;
    console.log(e)
    if (e.detail.errMsg == "getUserInfo:ok") {
      var data = {
        nickname: e.detail.userInfo.nickName,
        sex: e.detail.userInfo.gender,
        avatar: e.detail.userInfo.avatarUrl
      }
      app.memberModify(data);
      that.personalInfo();

    }
  },
  //跳转余额页面
  onBalanceTap: function(e) {
    wx.navigateTo({
      url: '../personal/balance/balance',
    })
  },
  //跳转优惠券页面
  onCouponTap: function(e) {
    wx.navigateTo({
      url: '../personal/personalcoupon/personalcoupon',
    })
  },
  //跳入我的订单
  onMyorderTap: function(e) {
    wx.navigateTo({
      url: '../personal/myorder/myorder?stateid=' + e.currentTarget.dataset.stateid + "&stateindex=" + e.currentTarget.dataset.stateindex,
    })
  },
  //跳入我的订单待售后单独页面
  onMyOrderSaleTap: function(e) {
    wx.navigateTo({
      url: '../personal/myordersale/myordersale?stateid=' + e.currentTarget.dataset.stateid
    })
  },

  //跳入物流进度
  onLogisticsProgressTap: function(e) {
    wx.navigateTo({
      url: '../personal/logisticsprogress/logisticsprogress?orderid=' + e.currentTarget.dataset.orderid
    })
  },
  //我要签到
  signin: function() {
    var that = this;
    utils.http('/api/v1/member/signin', {}, function() {
      wx.showToast({
        title: '签到成功~',
        success: function() {
          that.personalInfo();
        }
      })
    });
  },
  myIntegralLog: function() {
    wx.navigateTo({
      url: '/pages/myIntegralLog/myIntegralLog?type=1',
    })
  },
  mynotice: function() {
    wx.navigateTo({
      url: '/pages/personal/mynotice/mynotice',
    })
  }
})